<?php

namespace App\Admin\Controllers;

use App\Models\Pictuer;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PictuersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Pictuer);

        $grid->id('Id');
        $grid->column('category.name', '分类');
        //$grid->path('Path');
        $grid->domain('所在服务器')->display(function ($domain) {
            return $domain ?: '本地';
        });
        $grid->filename('文件名');
        $grid->mime('图片类型')->display(function () {
            if($this->mime) return $this->mime;
            return $this->ext;
        });
        $grid->rgb('色调')->display(function ($rgb) {
          return '<span style="background-color: #' . str_replace('0x', '', $rgb) . ';width: 3rem; height: 3rem; display:block;"></span>';
        });
        $grid->title('标题')->editable();
        $grid->width('宽度');
        $grid->height('高度');
        $grid->image('图片')->display(function () {
            $url = '';
            if ($this->domain) $url = '//' . $this->domain . '/' . $this->path . '/' . $this->filename;
            else $url = '/storage/uploads/' . $this->path . '/' . $this->filename;
            return '<img style="width:120px; height:120px;" src="' . $url . '"/>';
        });

        //$grid->crawler_pictuer_id('Crawler pictuer id');
        //$grid->article_id('Article id');
        $grid->created_at('添加时间')->sortable();
        $grid->updated_at('最后修改')->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Pictuer::findOrFail($id));

        $show->id('Id');
        $show->path('Path');
        $show->domain('Domain');
        $show->filename('Filename');
        $show->title('Title');
        $show->width('Width');
        $show->height('Height');
        $show->category_id('Category id');
        $show->crawler_pictuer_id('Crawler pictuer id');
        $show->article_id('Article id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Pictuer);

        $form->text('path', 'Path');
        $form->text('domain', 'Domain');
        $form->text('filename', 'Filename');
        $form->text('title', 'Title');
        $form->number('width', 'Width');
        $form->number('height', 'Height');
        $form->number('category_id', 'Category id');
        $form->number('crawler_pictuer_id', 'Crawler pictuer id');
        $form->number('article_id', 'Article id');

        return $form;
    }
}
